/*
 * This file is part of andExplorer, android.ovhoo.com
 * Copyright (C) 2007 Mohamed ZALIM <mzalim@ovhoo.com>
 *
 * andExplorer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * andExplorer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */
package com.ovhoo.android.file;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import android.net.Uri;



/**
 * Adaptation of the class File to add some usefull functions
 * @author mzalim
 *
 */
public class FileInterface{
	
	public static enum fileSizeUnit {FILE_SIZE_BITS, FILE_SIZE_BYTES, FILE_SIZE_KIB, FILE_SIZE_MIB, FILE_SIZE_GIB, FILE_SIZE_TIB};
	
	
	private File file;
	
	public FileInterface(File file) {
		this.file = file;
	}
	
	public FileInterface(String  path) {
		this.file = new File(path);
	}
	
	
	public String getParentDir(){
		String _parent = this.file.getParent();
		if (_parent == null){
			return "/";
		}else{
			return _parent;
		}
		
	}
	
	public ArrayList<FileInterface> ls(){
		if (this.file.isDirectory()){
			ArrayList<FileInterface> _retour = new ArrayList<FileInterface>();
			java.io.File _files[] = this.file.listFiles();
			for(int _index=0; _index < _files.length; _index ++){
				_retour.add(new FileInterface(_files[_index]));
			}
			
			return _retour;
		}
		else{
			return null;
		}
	}
	
	public ArrayList<FileInterface> ls(FilenameFilter filter){
		if (this.file.isDirectory()){
			ArrayList<FileInterface> _retour = new ArrayList<FileInterface>();
			java.io.File _files[] = this.file.listFiles(filter);
			
			for(int _index=0; _index < _files.length; _index ++){
				_retour.add(new FileInterface(_files[_index]));
			}
			
			return _retour;
		}
		else{
			return null;
		}
	}
	
	/**
	 * Returns the file size in human readable units
	 * Defaut unit is in Bytes
	 * @return file size as String
	 */
	public String getFileSize(fileSizeUnit unit){
		long _size = this.file.length();
		if (unit == null){
			return _size + " Bytes";
		}
		
		switch(unit){
		case FILE_SIZE_BITS : return _size*8 + " Bits";
		case FILE_SIZE_KIB : return _size/1024 + " KIB";
		case FILE_SIZE_MIB : return _size/1048576 + " MIB";
		case FILE_SIZE_GIB : return _size/1073741824 + " GIB";
		
		default: return _size + " Bytes";
		}
		
	}
	
	/**
	 * Returns the file size in human readable units
	 * calculate the best unit to use
	 */
	public String getFileSize(){
		long _size = this.file.length();
		
		if (_size>2073741824){
			 return _size/1073741824 + " GIB";
		}
		else if (_size>10485760){
			return _size/1048576 + " MIB";
		}
		else if (_size>10240){
			return _size/1024 + " KIB";
		}
		else if (_size>10){
			return _size + " Bytes";
		}
		else{
			return _size*8 + " Bits";
		}
	}
	
	public String getName(){
		return this.file.getName();
	}
	
	public boolean isDirectory(){
		return this.file.isDirectory();
	}
	
	public String getPath(){
		return this.file.getPath();
	}
	
	public String getAbsolutePath(){
		return this.file.getAbsolutePath();
	}
	
	public boolean mkdirs(){
		return this.file.mkdirs();
	}

	public boolean renameTo(String name) {
		File _file= new File(this.getParentDir() + "/" + name);
		return this.file.renameTo(_file);
	}

	public boolean delete() {
		return this.file.delete();
	}

	public Uri getUri() {
		return Uri.parse("file://"+ this.file.getAbsolutePath());
	}
	
	
	public boolean equals(Object obj){
		if (obj instanceof FileInterface){
			return this.file.equals(((FileInterface)obj).file);
		}
		else{
			return false;
		}
		
		
	}

	public File getFile() {
		return this.file;
	}
}
